package com.note.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.note.pojo.*;
import com.note.service.CommService;
import com.note.service.NotLikeService;
import com.note.service.NoteService;
import com.note.service.UserService;
import com.note.vo.NoteConZan;
import com.note.vo.TComm;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.stream.Collectors;

@Controller
@RequestMapping("/note")
public class NoteController {
    @Autowired
    private NoteService noteService;
    @Autowired
    private CommService commService;
    @Autowired
    private UserService userService;
    @Autowired
    private NotLikeService notLikeService;

    @RequestMapping("/add")
    public String add(Note note, HttpSession session){
        noteService.insertNote(note,session);
        return "addsuc";
    }

    @RequestMapping("/getnotebyid")
    public String getnotebyid(Integer id, Model model){
        Note note = noteService.getNoteById(id);
        model.addAttribute("note",note);
        return "notecon";
    }

    @RequestMapping("/del")
    public String del(Integer id,HttpSession session){
        noteService.deleteNoteById(id);
        return "forward:/index";
    }

    @RequestMapping("/del0")
    public String del0(Integer id,HttpSession session){
        noteService.deleteNoteById(id);
        return "forward:/note/notesit?type=0";
    }

    @RequestMapping("/del1")
    public String del1(Integer id,HttpSession session){
        noteService.deleteNoteById(id);
        return "forward:/note/notesit?type=1";
    }
    @RequestMapping("/del2")
    public String del2(Integer id,HttpSession session){
        noteService.deleteNoteById(id);
        return "forward:/note/notesit?type=2";
    }

    @RequestMapping("/notesit")
    public String index(@RequestParam(value = "pn",defaultValue = "1") Integer pn, int type, HttpSession session, Model model){
        PageHelper.startPage(pn,6);
        User user = (User) session.getAttribute("user");
        Integer userid= user.getId();
        List<Note> notes = noteService.listByUidTy(userid,type);
        PageInfo pageInfo=new PageInfo(notes,5);
        model.addAttribute("pageInfo",pageInfo);
        if (type==0){
            return "listnote";
        }if (type==1){
            return "listnote1";
        }
        return "listnote2";
    }
    //分享
    @RequestMapping("/share")
    public String share(Integer id,HttpSession session){
        int types=1;
        noteService.alterType(id,types);

        return "redirect:/note/notesit?type=0";
    }
    //设为个人隐私
    @RequestMapping("/priv")
    public String priv(Integer id,HttpSession session){
        int types=0;
        noteService.alterType(id,types);

        return "redirect:/note/notesit?type=1";
    }
    //到修改笔记页面
    @RequestMapping("/toalterjsp")
    public String toalterjsp(Integer id,Model model){
        Note note = noteService.getNoteById(id);
        model.addAttribute("note",note);
        return "alterbj";
    }

    //到修改笔记页面
    @RequestMapping("/toalterjsp1")
    public String toalterjsp1(Integer id,Model model){
        Note note = noteService.getNoteById(id);
        model.addAttribute("note",note);
        return "alterbj1";
    }

    //修改笔记
    @RequestMapping("/alternote")
    public String alterNote(Integer id,Note note){
        note.setId(id);
        noteService.alterNote(note);
        int type=noteService.getNoteById(id).getType();
        if(type==1){
            return "redirect:/note/notesit?type=1";
        }if (type==0){
            return "redirect:/note/notesit?type=0";
        }
        return "redirect:/note/notesit?type=2";
    }

    //单独修改页面修改笔记
    @RequestMapping("/alternote1")
    public String alterNote1(Integer id,Note note){
        note.setId(id);
        noteService.alterNote(note);
        return "redirect:/onlyalter";
    }

    //点击获取问题全文
    @RequestMapping("/detail")
    public String detail(Integer id, Model model){
        Note note = noteService.getNoteById(id);
        //获取该问题的回复
        List<Comm> coms1 = commService.getComsByNoteId(id);

        List<TComm> coms = coms1.stream().map(comm1 -> {
            TComm comm = new TComm();
            BeanUtils.copyProperties(comm1,comm);
            comm.setId(comm1.getId());
            comm.setContents(comm1.getContents());
            comm.setCreatetime(comm1.getCreatetime());
            comm.setNoteid(comm1.getNoteid());
            comm.setLikes(comm1.getLikes());
            if (comm1.getUserid()==null){
                comm.setLoginname("游客");
                comm.setAvatar("upload/youke.jpg");
            }else {
                comm.setLoginname(userService.findNameById(comm1.getUserid()));
                comm.setAvatar(userService.findAvatarById(comm1.getUserid()));
            }
            return comm;
        }).collect(Collectors.toList());
        String zuozhe = userService.findNameById(note.getUserid());
        String avatar = userService.findAvatarById(note.getUserid());
        model.addAttribute("note",note);
        model.addAttribute("zuozhe",zuozhe);
        model.addAttribute("touxiang",avatar);
        model.addAttribute("coms",coms);
        return "noteDetail";
    }

    @ResponseBody
    @RequestMapping("/zan")
    public int zan(Integer noteid,HttpSession session){
        User user = (User) session.getAttribute("user");
        if (user!=null){
            NotLikes notLike=new NotLikes();
            notLike.setNoteid(noteid);
            notLike.setUserid(user.getId());
            NotLikes notLikes = notLikeService.getByUidCid(notLike);
            if (notLikes!=null){
                notLikes.setNum(notLikes.getNum()+1);
                notLikeService.addoneNlikes(notLikes);
            }else {
                notLikeService.addNlikes(notLike);
            }
        }else {
            NotLikes notLike=new NotLikes();
            notLike.setNoteid(noteid);
            notLike.setUserid(0);
            NotLikes notLikes = notLikeService.getByUidCid(notLike);
            if (notLikes!=null){
                notLikes.setNum(notLikes.getNum()+1);
                notLikeService.addoneNlikes(notLikes);
            }else {
                notLikeService.addNlikes(notLike);
            }
        }
        int likes = notLikeService.sumNlikes(noteid);
        return likes;
    }

    @RequestMapping("/listnoteincom")
    public String listnoteincom(@RequestParam(value = "pn",defaultValue = "1") Integer pn,HttpSession session,Model model){
        int size =(int) Math.ceil((double) noteService.getNotesByType(1).size()/8);
        int[] sizes=new int[size];
        for (int i = 0; i < sizes.length ; i++) {
            sizes[i]=i+1;
        }
        PageHelper.startPage(pn,8);
        List<Note> noteList=noteService.getNotesByType(1);
        List<NoteConZan> notes=noteList.stream().map(note1 -> {
            NoteConZan note=new NoteConZan();
            BeanUtils.copyProperties(note1,note);
            note.setId(note1.getId());
            note.setContent(note1.getContent());
            note.setCreatetime(note1.getCreatetime());
            note.setTitle(note1.getTitle());
            note.setType(note1.getType());
            note.setLikes(notLikeService.sumNlikes(note1.getId()));
            note.setComnum(commService.getComsByNoteId(note1.getId()).size());
            return note;
        }).collect(Collectors.toList());
        PageInfo pageInfo=new PageInfo(notes,size);
        pageInfo.setPageNum(pn);
        pageInfo.setNavigatepageNums(sizes);
        pageInfo.setPages(size);
        model.addAttribute("pageInfo",pageInfo);
        return "listnoteincom";
    }
}
